#!/usr/bin/env bash

set -euo pipefail
root_dir=$(realpath "$(dirname "$(realpath "$0")")/..")

docker build -t tikv-dashboard-gen -f - . <<EOF
FROM pyfound/black:23.11.0

RUN pip install isort grafanalib==v0.7.0

CMD ["bash"]
EOF

docker run \
    --rm \
    -v "$root_dir/metrics:/metrics" \
    -e PYTHONDONTWRITEBYTECODE=1 \
    tikv-dashboard-gen \
    bash -c "
        set -euo pipefail
        isort --profile black /metrics/grafana/*.py
        black /metrics/grafana/*.py
        for dashboard in /metrics/grafana/*.dashboard.py; do
            name=\$(basename \$dashboard .dashboard.py)
            generate-dashboard \
                -o /metrics/grafana/\$name.json \
                /metrics/grafana/\$name.dashboard.py
            sha256sum ./metrics/grafana/\$name.json > /metrics/grafana/\$name.json.sha256
        done
    "
